package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Product_type;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.Product_typeService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: May
 * @Date: 2024/12/3 12:58
 * @Version: 1.0
 */
@RestController
@RequestMapping("/product_type")
@AllArgsConstructor
@ResponseResult
public class Product_typeController {

    private final Product_typeService productTypeService;

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String product_type_name = req.getParameter("product_type_name");
        String product_type_desc = req.getParameter("product_type_desc");
        String product_type_icon = req.getParameter("product_type_icon");
        Product_type productType = new Product_type(id,
                product_type_name,
                product_type_desc,
                product_type_icon);
        Boolean save = productTypeService.save(productType);
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获得id
        String id = req.getParameter("id");
        // 查询商品
        Product_type product = productTypeService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(product);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询用户
        List<Product_type> productTypes = productTypeService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(productTypes);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Boolean remove = productTypeService.remove(id);
        return ResponseVo.buildSuccess(remove);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String product_type_name = req.getParameter("product_type_name");
        String product_type_desc = req.getParameter("product_type_desc");
        String product_type_icon = req.getParameter("product_type_icon");
        Boolean update = productTypeService.update(new Product_type(id,
                product_type_name,
                product_type_desc,
                product_type_icon));
        return ResponseVo.buildSuccess(update);
    }

    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        //查询用户
        PageEntity<Product_type> productTypePageEntity = productTypeService.findUserPage(Integer.valueOf(current),Integer.valueOf(pageSize));
        //发送给客户端
        return ResponseVo.buildSuccess(productTypePageEntity);
    }

    @RequestMapping("/findByNameContaining")
    public ResponseVo findByNameContaining(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String name = req.getParameter("name");
        //查询用户
        List<Product_type> productTypes = productTypeService.findByNameContaining(name);
        //发送给客户端
        return ResponseVo.buildSuccess(productTypes);
    }

}
